﻿using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _arbors
    {
        public static void arbors(Dictionary<string, Object> data)  //锥度铰刀
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            //作图
            //参数
             double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            double scale = 1;

            /*double oal = 220;
            double lu = 140;
            double l = 40;
            double d1 = 10;
            double d2 = 18;
            double czc = 2;
            double db = 30;
            double ds = 25;
            double tg = 0.05;*/

            double oal = Convert.ToDouble(data["oal"]) * scale;
            double lu = Convert.ToDouble(data["lu"]) * scale;
            double l = Convert.ToDouble(data["l"]) * scale;
            double d1 = Convert.ToDouble(data["dconms"]) * scale;
            double d2 = Convert.ToDouble(data["d2"]) * scale;
            double czc = Convert.ToDouble(data["czc"]);
            double db = Convert.ToDouble(data["db"]) * scale;
            double ds = Convert.ToDouble(data["ds"]) * scale;
            double tg = Convert.ToDouble(data["tg"]);
            double ta = 1.909683 * pi / 180;

            if (czc == 2)
            {
                db = 17.78 * scale;
                ds = 14.6 * scale;
                tg = 0.04995;
            }
            if (czc == 3)
            {
                db = 23.825 * scale;
                ds = 19.8 * scale;
                tg = 0.05020;
            }
            if (czc == 4)
            {
                db = 31.267 * scale;
                ds = 25.9 * scale;
                tg = 0.05194;
            }
            if (czc == 5)
            {
                db = 44.399 * scale;
                ds = 37.6 * scale;
                tg = 0.05263;
            }
            double dn = db - 0.5;
            if (d2 < db)
            {
                dn = d2 - 0.5;
            }
            //坐标
            Vector2 v1 = new Vector2(0, ds / 2 - 0.5);
            Vector2 v2 = new Vector2( Math.Sqrt(0.25 / (1 + tg * tg / 4)), ds / 2 + Math.Sqrt(0.25 / (1 + tg * tg / 4)) * tg / 2);
            Vector2 v3 = new Vector2((oal-lu), db / 2);
            Vector2 v4 = new Vector2(v3.X + v3.Y - dn / 2, dn / 2);
            Vector2 v5 = new Vector2(v4.X + (lu - l)*8 / 9, v4.Y);
            Vector2 v6 = new Vector2(v5.X, d2 / 2);
            Vector2 v7 = new Vector2(oal - l, v6.Y);
            Vector2 v8 = new Vector2(v7.X, d2 / 10);
            Vector2 v9 = new Vector2(v8.X + (v7.X - v6.X) / 3,v8.Y );
            Vector2 v10 = new Vector2(v7.X, d1 / 2);
            Vector2 v11 = new Vector2(v10.X + (l/20), v10.Y);
            Vector2 v12 = new Vector2(v10.X + l - 0.5, v11.Y - (l - (l / 20) - 0.5) * Math.Tan(ta / 2));
            Vector2 v13 = new Vector2(v12.X + 0.5, v12.Y - 0.5);
            Vector2 a1 = new Vector2(v1.X, v1.Y * -1);
            Vector2 a2 = new Vector2(v2.X, v2.Y * -1);
            Vector2 a3 = new Vector2(v3.X, v3.Y * -1);
            Vector2 a4 = new Vector2(v4.X, v4.Y * -1);
            Vector2 a5 = new Vector2(v5.X, v5.Y * -1);
            Vector2 a6 = new Vector2(v6.X, v6.Y * -1);
            Vector2 a7 = new Vector2(v7.X, v7.Y * -1);
            Vector2 a8 = new Vector2(v8.X, v8.Y * -1);
            Vector2 a9 = new Vector2(v9.X, v9.Y * -1);
            Vector2 a10 = new Vector2(v10.X, v10.Y * -1);
            Vector2 a11 = new Vector2(v11.X, v11.Y * -1);
            Vector2 a12 = new Vector2(v12.X, v12.Y * -1);
            Vector2 a13 = new Vector2(v13.X, v13.Y * -1);
            Vector2 v14 = new Vector2(v1.X - 8, 0);
            Vector2 v15 = new Vector2(v14.X + oal + 16, v14.Y);


            LwPolyline line1 = new LwPolyline();
            line1.Vertexes.Add(new LwPolylineVertex(a2));
            line1.Vertexes.Add(new LwPolylineVertex(v2));
            line1.Vertexes.Add(new LwPolylineVertex(v3));
            line1.Vertexes.Add(new LwPolylineVertex(a3));
            line1.Vertexes.Add(new LwPolylineVertex(a2));
            line1.Vertexes.Add(new LwPolylineVertex(a1));
            line1.Vertexes.Add(new LwPolylineVertex(v1));
            line1.Vertexes.Add(new LwPolylineVertex(v2));
            fun.PolyFill(dxf, line1, 0.3, contour);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v3));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(a5));
            line2.Vertexes.Add(new LwPolylineVertex(a4));
            line2.Vertexes.Add(new LwPolylineVertex(a3));
            line2.Vertexes[0].Bulge = Math.Tan(90 * pi / 180 / 4);
            line2.Vertexes[4].Bulge = Math.Tan(90 * pi / 180 / 4);
            fun.PolyFill(dxf, line2, 0.3, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v6));
            line3.Vertexes.Add(new LwPolylineVertex(v7));
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            line3.Vertexes.Add(new LwPolylineVertex(v9));
            line3.Vertexes.Add(new LwPolylineVertex(a9));
            line3.Vertexes.Add(new LwPolylineVertex(a8));
            line3.Vertexes.Add(new LwPolylineVertex(a7));
            line3.Vertexes.Add(new LwPolylineVertex(a6));
            line3.Vertexes.Add(new LwPolylineVertex(v6));
            line3.Vertexes[3].Bulge = Math.Tan(-20 * pi / 180 / 4);
            fun.PolyFill(dxf, line3, 0.3, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v10));
            line4.Vertexes.Add(new LwPolylineVertex(v11));
            line4.Vertexes.Add(new LwPolylineVertex(v12));
            line4.Vertexes.Add(new LwPolylineVertex(v13));
            line4.Vertexes.Add(new LwPolylineVertex(a13));
            line4.Vertexes.Add(new LwPolylineVertex(a12));
            line4.Vertexes.Add(new LwPolylineVertex(v12));
            line4.Vertexes.Add(new LwPolylineVertex(a12));
            line4.Vertexes.Add(new LwPolylineVertex(a11));
            line4.Vertexes.Add(new LwPolylineVertex(a10));
            fun.PolyFill(dxf, line4, 0.3, contour);

            Line line5 = new Line(v14, v15);
            line5.Layer = centre;
            line5.Linetype = Linetype.DashDot;
            line5.LinetypeScale = 3;
            dxf.AddEntity(line5);
            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 3.5;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = 3.5;
            double offset = 10;
            //线段尺寸标注
            //尺寸1
            LinearDimension dim1 = new LinearDimension(new Line(a1, a13), offset * 3 , 180, dimensionStyle);
            dim1.UserText = "OAL = " + Math.Round(oal / scale, 2);
            dxf.AddEntity(dim1);
            //尺寸2
            LinearDimension dim2 = new LinearDimension(new Line(a3, a13), offset * 2, 180, dimensionStyle);
            dim2.UserText = "LU = " + Math.Round(lu / scale, 2);
            dxf.AddEntity(dim2);
            //尺寸3
            LinearDimension dim3 = new LinearDimension(new Line(a7, a13), offset, 180, dimensionStyle);
            dim3.UserText = "L = " + Math.Round(l / scale, 2);
            dxf.AddEntity(dim3);
            //尺寸4
            LinearDimension dim4 = new LinearDimension(new Line(v6, a6), offset, 90, dimensionStyle);
            dim4.UserText = "D2 = " + Math.Round(d2 / scale, 2);
            dxf.AddEntity(dim4);
            //尺寸5
            LinearDimension dim5 = new LinearDimension(new Line(v11, a11), offset, -90, dimensionStyle);
            dim5.UserText = "D1 = " + Math.Round(d1 / scale, 2);
            dxf.AddEntity(dim5);

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}